from pathlib import Path
from yaml import safe_load
from pydantic import BaseModel

BASE_DIR = Path.cwd()
DATA_DIR = BASE_DIR / "data"
CONFIG_DIR = BASE_DIR / "config"
STATIC_DIR = BASE_DIR / "static"

DATA_DIR.mkdir(parents=True, exist_ok=True)


class LLMConfig(BaseModel, extra="ignore"):
    """LLM配置类"""

    name: str | None = None
    key: str | None = None
    url: str | None = None
    model: str | None = None
    multi_modal: bool = False
    """是否支持多模态"""
    supports_functools: bool = False
    """是否支持functools"""

    def __bool__(self) -> bool:
        return bool(self.key and self.url and self.model and self.name)


class GlobalConfig(BaseModel, extra="allow"):
    """全局配置类"""

    llm_configs: list[LLMConfig] = []
    answer_url: str = "http://127.0.0.1:18000"
    cache_answer: bool = True


global_config = GlobalConfig()

# 检查llm_configs是否为空

if not all(global_config.llm_configs):
    raise ValueError("LLM配置不能为空，请在配置文件中添加LLM配置。")

if CONFIG_DIR.joinpath("config.dev.yaml").exists():
    global_config = GlobalConfig.model_validate(
        safe_load(CONFIG_DIR.joinpath("config.dev.yaml").read_text(encoding="utf-8"))
    )
elif CONFIG_DIR.joinpath("config.yaml").exists():
    global_config = GlobalConfig.model_validate(
        safe_load(CONFIG_DIR.joinpath("config.yaml").read_text(encoding="utf-8"))
    )
else:
    raise FileNotFoundError(
        "配置文件 config.dev.yaml 或 config.yaml 不存在，请创建一个配置文件。"
    )
